package org.mines.douai.j2ee.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;

import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.mines.douai.j2ee.webservices.WeatherWebService;



@WebServlet(name = "SimpleWeather", urlPatterns = { "/SimpleWeather" })
public class SimpleWeatherServlet extends HttpServlet {

	public void service (ServletRequest request, ServletResponse response) throws IOException,ServletException {

		//Get the temperature value from the webservice
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		factory.getInInterceptors().add(new LoggingInInterceptor());
		factory.getOutFaultInterceptors().add(new LoggingOutInterceptor());
		factory.setServiceClass(WeatherWebService.class);
		factory.setAddress("http://localhost:9000/TpWebService/services/WeatherWebServiceImplPort");
		WeatherWebService client = (WeatherWebService) factory.create();

		String usa = client.getWeatherValue("us");
		
		String france = client.getWeatherValue("fr");
		
		String angleterre = client.getWeatherValue("en");
		
		
		// generation of the view
		response.setContentType("text/html");// Type MIME
		java.io.PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"fr\" lang=\"fr\">"
				+ "<head>"
				+ "<title>Tp servlet 1</title>"
				+ "</head>"
				+ "<body>");
		
		
		out.println("<p>Aux Etats-Unis il fait "+usa+"</p>");
		out.println("<p>En France il fait "+france+"</p>");
		out.println("<p>En Angleterre il fait "+angleterre+"</p>");
		
		out.println("</body>");
		out.println("</html>");

		
		

	}

}
